﻿Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Web

Public Class SaleModiSP
    Inherits BusinessDomainBase

    Private _businessGUID As String     '销售变更申请GUID
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/slxt/SaleModiSP.xml")
        _xml.LoadXml(MyBase.XML)
    End Sub

    '属性：销售变更申请GUID
    Public Overrides Property BusinessGUID() As String
        Get
            Return _businessGUID
        End Get
        Set(ByVal Value As String)
            _businessGUID = Value
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return _xml.OuterXml
        End Get
        Set(ByVal Value As String)
            _xml.LoadXml(Value)
        End Set
    End Property

    '功能：取销售变更申请数据，填充XML
    Public Overrides Function LoadData() As Integer

        Dim strSQL As String
        Dim dtTemp As DataTable

        '主记录    
        strSQL = "SELECT s.SaleModiApplyGUID, s.SaleGUID, s.SaleType, s.ApplyType, s.ReasonSort, s.Reason, " & _
                 " s.ApplyBy, s.ApplyDate, s.BUGUID, s.ProjGUID, s.RoomGUID, s.ApproveState, s.ApplyState, " & _
                 " m.CalMode, m.PayformName, m.DiscntValue, m.RoomTotal, m.RmbCjTotal" & _
                 " FROM s_SaleModiApply  s left join s_SModiInfo m " & _
                 " ON s.SaleModiApplyGUID=m.SaleModiApplyGUID " & _
                 " WHERE s.SaleModiApplyGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then
            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID

            '主记录
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售变更申请GUID']").InnerText = dtTemp.Rows(0).Item("SaleModiApplyGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售单GUID']").InnerText = dtTemp.Rows(0).Item("SaleGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售单类型']").InnerText = dtTemp.Rows(0).Item("SaleType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请类型']").InnerText = dtTemp.Rows(0).Item("ApplyType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原因分类']").InnerText = dtTemp.Rows(0).Item("ReasonSort").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原因说明']").InnerText = dtTemp.Rows(0).Item("Reason").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请人']").InnerText = dtTemp.Rows(0).Item("ApplyBy").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请日期']").InnerText = dtTemp.Rows(0).Item("ApplyDate").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='单位GUID']").InnerText = dtTemp.Rows(0).Item("BUGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='项目GUID']").InnerText = dtTemp.Rows(0).Item("ProjGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间GUID']").InnerText = dtTemp.Rows(0).Item("RoomGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间信息']").InnerText = MyDB.GetDataItemString("SELECT RoomInfo FROM ep_room WHERE RoomGUID='" & dtTemp.Rows(0).Item("RoomGUID").ToString & "'")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='审批状态']").InnerText = dtTemp.Rows(0).Item("ApproveState").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请单状态']").InnerText = dtTemp.Rows(0).Item("ApplyState").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='计价方式']").InnerText = dtTemp.Rows(0).Item("CalMode").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='付款方式']").InnerText = dtTemp.Rows(0).Item("PayformName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='折扣']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("DiscntValue")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间总价']").InnerText = dtTemp.Rows(0).Item("RoomTotal").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='协议总价']").InnerText = dtTemp.Rows(0).Item("RmbCjTotal").ToString

        End If

        Return 0

    End Function

    '功能：发起“销售变更”的审批流程
    Public Overrides Function Initiate() As String

        Dim strSQL As String
        Dim strResult As String

        strSQL = "UPDATE s_SaleModiApply SET ApproveState = '审批中' WHERE SaleModiApplyGUID = '" & Me._businessGUID & "'"

        '提交更新语句
        If MyDB.ExecSQL(strSQL) > 0 Then
            strResult = "<xml result=""true"" errormessage=""""></xml>"
        Else
            strResult = "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        Return strResult

    End Function

    '功能：根据XML进行数据对“销售变更”进行归档
    Public Overrides Function SaveData() As String

        Dim strSQL As String
        Dim strResult As String
        Dim strApproveBy As String = HttpContext.Current.Session("UserName")

        strSQL = "UPDATE s_SaleModiApply SET" & _
                 " ApproveState='已审批'," & _
                 " IsApproved='同意'," & _
                 " ApproveBy='" & strApproveBy & "'," & _
                 " ApproveDate=GetDate()" & _
                 " WHERE SaleModiApplyGUID = '" & Me._businessGUID & "'"

        '提交更新语句
        If MyDB.ExecSQL(strSQL) > 0 Then
            strResult = "<xml result=""true"" errormessage=""""></xml>"
        Else
            strResult = "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        Return strResult

    End Function

    '功能：不通过处理
    Public Overrides Function SaveData_NoPass() As String

        Dim strSQL As String
        Dim strResult As String
        Dim strApproveBy As String = HttpContext.Current.Session("UserName")

        strSQL = "UPDATE s_SaleModiApply SET " & _
                 "ApproveState='已审批'," & _
                 "IsApproved='不同意'," & _
                 "ApproveBy='" & strApproveBy & "'," & _
                 "ApproveDate=GetDate() " & _
                 "WHERE SaleModiApplyGUID = '" & Me._businessGUID & "'"

        '提交更新语句
        If MyDB.ExecSQL(strSQL) > 0 Then
            strResult = "<xml result=""true"" errormessage=""""></xml>"
        Else
            strResult = "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        Return strResult

    End Function

    '功能：审批作废处理
    Public Overrides Function SaveData_Cancel() As String

        Dim strSQL As String
        Dim strResult As String

        strSQL = "UPDATE s_SaleModiApply SET ApproveState = '待审批' WHERE SaleModiApplyGUID = '" & Me._businessGUID & "'"

        '提交更新语句
        If MyDB.ExecSQL(strSQL) Then
            strResult = "<xml result=""true"" errormessage=""""></xml>"
        Else
            strResult = "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        '返回执行是否成功的信息
        Return strResult

    End Function

    '功能：发起撤回“销售变更”的审批流程
    Public Overrides Function InitiateWithDrawn() As String

        '由于发起撤回的审批流程和审批作废处理是一样的，所以直接调用审批作废的方法即可
        Return SaveData_Cancel()

    End Function

End Class
